<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

use \Firebase\JWT\JWT;
use \Firebase\JWT\Key;

// 解密user_token
function decoded_user_token($token = '')
{
    
    try {
        
        $decoded = JWT::decode($token, new Key(API_KEY . JWT_KEY, 'HS256'));

        return (array) $decoded;
        
    } catch (Exception $ex) {
        
        return $ex->getMessage();
    }
}

// 获取解密信息中的data
function get_member_by_token($token = '')
{
    
    $result = decoded_user_token($token);

    return is_array($result) && isset($result['data']) ? (array)$result['data'] : [];
}

// 获取普通用户token信息
function get_user_by_token($token = '')
{
    $result = decoded_user_token($token);
    return is_array($result) && isset($result['data']) ? (array)$result['data'] : [];
}

// 数据验签时数据字段过滤
function sign_field_filter($data = [])
{
    
    $data_sign_filter_field_array = config('data_sign_filter_field');
    
    foreach ($data_sign_filter_field_array as $v)
    {
        
        if (array_key_exists($v, $data)) {
            
            unset($data[$v]);
        }
    }
    
    return $data;
}

// 过滤后的数据生成数据签名
function create_sign_filter($data = [], $key = '')
{
    
    $filter_data = sign_field_filter($data);
    
    return empty($key) ? data_md5_key($filter_data, API_KEY) : data_md5_key($filter_data, $key);
}

// 普通用户JWT Token签发函数
function user_token_sign($user = [], $expire = 2592000)
{
    if (empty($user) || !isset($user['user_id'])) {
        throw new \InvalidArgumentException('用户信息不能为空且必须包含user_id');
    }

    $payload = [
        'iss' => 'OneBase',
        'aud' => 'user',
        'iat' => time(),
        'exp' => time() + $expire,
        'data' => $user
    ];

    return JWT::encode($payload, API_KEY . JWT_KEY, 'HS256');
}
